force_ocr = False # OCR을 강제할지 여부 플래그
pdf_page_count = 0

# 3-1. PDF 텍스트 추출 시도 (PyMuPDF)
try:
    print("Attempting text extraction from PDF...")
    with fitz.open(stream=contents, filetype="pdf") as doc:
        pdf_page_count = len(doc)
        if doc.needs_pass:
            print("PDF is password protected. Text extraction skipped, proceeding to OCR.")
            force_ocr = True # 비밀번호가 있으면 OCR 강제
        else:
            all_text = ""
            for page_num in range(pdf_page_count):
                page = doc.load_page(page_num)
                page_text = page.get_text("text")
                all_text += page_text
                if page_num < pdf_page_count - 1:
                    all_text += "\n--- Page Break ---\n" # 페이지 구분 추가

            # 추출된 텍스트 유효성 검사
            if all_text and not all_text.isspace():
                print(f"Text extracted ({len(all_text)} chars). Validating content...")
                # 워터마크 제거 후 텍스트 확인
                cleaned_text = watermark_regex.sub("", all_text).strip()
                # 페이지 구분자도 제거하고 길이 계산
                cleaned_text_for_length = cleaned_text.replace("\n--- Page Break ---\n", "")

                # 1. 전체 텍스트 길이가 페이지 수 * 임계값보다 긴지 확인
                # 2. 워터마크 제거 후에도 텍스트가 남아있는지 확인
                min_total_length = MIN_MEANINGFUL_TEXT_LENGTH_PER_PAGE * pdf_page_count
                if len(cleaned_text_for_length) >= min_total_length and cleaned_text:
                    print("Meaningful text found after validation.")
                    extracted_text_from_pdf = all_text.strip() # 원본 추출 텍스트 저장
                    processing_method = "pdf_text_extraction"
                else:
                    print(f"Extracted text seems insufficient or mostly watermarks (Cleaned length: {len(cleaned_text_for_length)}, Threshold: {min_total_length}). Forcing OCR.")
                    force_ocr = True
            else:
                print("No text found via direct extraction. Proceeding to OCR.")
                force_ocr = True # 텍스트가 아예 없으면 OCR 강제

except Exception as e:
    print(f"Error during PDF text extraction: {e}. Proceeding to OCR.")
    force_ocr = True # 텍스트 추출 중 에러 발생 시 OCR 강제